package alva.com.getcamera.view.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import alva.com.getcamera.R;
import alva.com.getcamera.adapter.VideoRecyclerViewAdapter;
import alva.com.getcamera.bean.VideoBean;
import alva.com.getcamera.customview.MyDividerDecoration;
import alva.com.getcamera.utils.FileOperateUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoFragment extends Fragment implements VideoRecyclerViewAdapter.OnChildClickListener, SwipeRefreshLayout.OnRefreshListener, VideoRecyclerViewAdapter.OnChildLongClickListener {

    private RecyclerView videoRecyclerView;
    private SwipeRefreshLayout refreshLayout;
    private ArrayList<VideoBean> videoList = new ArrayList<>();
    private VideoRecyclerViewAdapter adapter;

    private final static String videoDirectory = "/storage/emulated/0/myVideo";
    private final static String videoFormat = ".mp4";

    public VideoFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        videoRecyclerView = (RecyclerView) view.findViewById(R.id.videoRecyclerView);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.v_refresh);
        adapter = new VideoRecyclerViewAdapter(getContext(), videoList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        videoRecyclerView.setLayoutManager(linearLayoutManager);
        videoRecyclerView.addItemDecoration(new MyDividerDecoration(getContext(), MyDividerDecoration.VERTICAL_LIST, R.drawable.decration_shape));
        videoRecyclerView.setItemAnimator(new DefaultItemAnimator());
        videoRecyclerView.setAdapter(adapter);

        adapter.setOnChildClickListener(this);
        adapter.setOnChildLongClickListener(this);
        refreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorGreen, R.color.colorPrimary, R.color.colorYellow);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.clear();
        adapter.addAll(FileOperateUtil.getVideoList(videoDirectory, videoFormat));
    }

    @Override
    public void onPause() {
        super.onPause();
        adapter.clear();
    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position, long id, String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(path), "video/mp4");
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        adapter.clear();
        adapter.addAll(FileOperateUtil.getVideoList(videoDirectory, videoFormat));
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onChildLongClick(RecyclerView parent, View view, final int position, long id, final String path) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("删除视频");
        builder.setMessage("确认删除这个视频?");
        builder.setCancelable(true);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                adapter.remove(position);
                FileOperateUtil.remove(path);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
